package com.aistar.controller;

import com.aistar.model.bo.Result;
import com.aistar.model.dto.resume.ResumeInfoDTO;
import com.aistar.model.entity.*;
import com.aistar.service.resume.IDeleteService;
import com.aistar.service.resume.IInsertService;
import com.aistar.service.resume.IResumeService;
import com.aistar.service.resume.impl.InsertServiceImpl;
import com.aistar.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @ClassName: ResumeRestController
 * @author: Lushengjie
 * @date: 2021/9/26 15:47
 * @Description: TODO...本类用来演示:
 */
@RestController
@RequestMapping("/resume")
public class ResumeRestController {
    @Autowired
    private IResumeService resumeService;

    @Autowired
    private IDeleteService deleteService;

    @Autowired
    private IInsertService insertService;
    //定义头像文件名
    String avatarName;
    @GetMapping("/update/resume_info")
    public Result updateResume(ResumeInfoDTO resumeInfoDTO) {

        Integer count = resumeService.updateResume(resumeInfoDTO);
        if (count != 0) {
            return new Result("200", "更新成功", resumeInfoDTO.getResume().getId());
        }

        return new Result("400", "更新失败");
    }
    //更新基础信息
    @PostMapping("/basic/update")
    public Result updateBasic(Integer id, String name, String sex, String resIdentity, String resState, String birthday, String area, String phone, String email) {

        if (null == name || name.trim().length() == 0) {
            return new Result("501", "请填写姓名");
        }
        if (null == birthday || birthday.trim().length() == 0) {
            return new Result("502", "请填写出生年月");
        }
        if (null == area || area.trim().length() == 0) {
            return new Result("503", "请填写所在城市");
        }
        if (null == phone || phone.trim().length() == 0) {
            return new Result("504", "请填写联系方式");
        }
        if (null == email || email.trim().length() == 0) {
            return new Result("505", "请填写邮箱");
        }
        int count = resumeService.updateByResume(id, name, sex, resIdentity, resState, birthday, area, phone, email,avatarName);
        if (count == 0) {
            return new Result("500", "错误了");
        }
        return new Result("200", "更新成功");
    }
    //更新求职偏好
    @PostMapping("/job/update")
    public Result updateJob(Integer id, String expectIndustry, String expectJob, String expectCity, String salaryRequirement) {
        if (null == expectIndustry || expectIndustry.trim().length() == 0) {
            return new Result("501", "请填写期望行业");
        }
        if (null == expectJob || expectJob.trim().length() == 0) {
            return new Result("502", "请填写期望职位");
        }
        if (null == expectCity || expectCity.trim().length() == 0) {
            return new Result("503", "请填写期望城市");
        }
        if (null == salaryRequirement || salaryRequirement.trim().length() == 0) {
            return new Result("504", "请填写期望薪资");
        }
        int count = resumeService.updateByResumeOfJob(id, expectIndustry, expectJob, expectCity, salaryRequirement);
        if (count == 0) {
            return new Result("500", "错误");
        }
        return new Result("200", "更新成功");
    }
    //更新教育经历
    @PostMapping("/education/update")
    public Result updateEducation(Integer id, String schoolName, String degree, String major, String startDate, String endDate, String experienceSchool) {
        System.out.println(id);
        if (null == schoolName || schoolName.trim().length() == 0) {
            return new Result("501", "请填写学校");
        }
        if (null == degree || degree.trim().length() == 0) {
            return new Result("502", "请填写学历");
        }
        if (null == major || major.trim().length() == 0) {
            return new Result("503", "请填写专业");
        }
        if ((null == startDate || startDate.trim().length() == 0) || (null == endDate || endDate.trim().length() == 0)) {
            return new Result("504", "请填写在校时间");
        }
        if (null == experienceSchool || experienceSchool.trim().length() == 0) {
            return new Result("505", "请填写在校经历");
        }
        Date start = DateUtil.parse(startDate,"yyyy-MM-dd");
        Date end = DateUtil.parse(endDate,"yyyy-MM-dd");
        if(end.getTime()<=start.getTime()){
            return new Result("506", "请填写正确的时间");
        }
        int count = resumeService.updateByEducation(id, schoolName, degree, major, startDate, endDate, experienceSchool);
        if (count == 0) {
            return new Result("500", "错误");
        }
        return new Result("200", "更新成功");
    }
    //更新实习经历
    @PostMapping("/intern/update")
    public Result updateIntern(Intern intern) {
        if (null == intern.getCompanyName() || intern.getCompanyName().trim().length() == 0) {
            return new Result("501", "请填写公司名称");
        }
        if (null == intern.getCompanyIndustry() || intern.getCompanyIndustry().trim().length() == 0) {
            return new Result("502", "请填写公司行业");
        }
        if (null == intern.getTitle() || intern.getTitle().trim().length() == 0) {
            return new Result("503", "请填写职位名称");
        }
        if ((null == intern.getStartDate() || intern.getStartDate().toString().trim().length() == 0) || (null == intern.getEndDate() || intern.getEndDate().toString().trim().length() == 0)) {
            return new Result("504", "请填写在校时间");
        }
        if (null == intern.getContent() || intern.getContent().trim().length() == 0) {
            return new Result("505", "请填写实习经历");
        }
        if(intern.getEndDate().getTime()<=intern.getStartDate().getTime()){
            return new Result("506", "请填写正确的时间");
        }
        int count = resumeService.updateByIntern(intern);
        if (count == 0) {
            return new Result("500", "错误");
        }
        return new Result("200", "更新成功");
    }
    //更新项目经历
    @PostMapping("/project/update")
    public Result updateProject(Project project) {
        if (null == project.getName() || project.getName().trim().length() == 0) {
            return new Result("501", "请填写项目名称");
        }
        if ((null == project.getStartTime() || project.getStartTime().toString().trim().length() == 0) || (null == project.getEndTime() || project.getEndTime().toString().trim().length() == 0)) {
            return new Result("502", "请填写项目时间");
        }
        if (null == project.getLink() || project.getLink().trim().length() == 0) {
            return new Result("503", "请填写项目链接");
        }
        if (null == project.getDescription() || project.getDescription().trim().length() == 0) {
            return new Result("504", "请填写项目描述");
        }
        if(project.getEndTime().getTime()<=project.getStartTime().getTime()){
            return new Result("506", "请填写正确的时间");
        }
        int count = resumeService.updateByProject(project);
        if (count == 0) {
            return new Result("500", "错误");
        }
        return new Result("200", "更新成功");
    }
    //更新获奖经历
    @PostMapping("/award/update")
    public Result updateAward(Award award) {
        if (null == award.getName() || award.getName().trim().length() == 0) {
            return new Result("501", "请填写获奖名称");
        }
        if (null == award.getAwardTime() || award.getAwardTime().toString().trim().length() == 0) {
            return new Result("502", "请填写获奖时间");
        }
        int count = resumeService.updateByAward(award);
        if (count == 0) {
            return new Result("500", "错误");
        }
        return new Result("200", "更新成功");
    }
    //更新个人总结
    @PostMapping("/summary/update")
    public Result updateSummary(Integer id, String summary) {
        if (null == summary || summary.trim().length() == 0) {
            return new Result("501", "请填写个人总结");
        }
        int count = resumeService.updateByResumeOfSummary(id, summary);
        if (count == 0) {
            return new Result("500", "错误");
        }
        return new Result("200", "更新成功");
    }
    //添加教育经历
    @PostMapping("/education/insert")
    public Result insertEducation(Education education){
//        System.out.println("简历id:"+resumeId);
//        System.out.println(education);
        if (null == education.getSchoolName() || education.getSchoolName().trim().length() == 0) {
            return new Result("501", "请填写学校");
        }
        if (null == education.getDegree() || education.getDegree().trim().length() == 0) {
            return new Result("502", "请填写学历");
        }
        if (null == education.getMajor() || education.getMajor().trim().length() == 0) {
            return new Result("503", "请填写专业");
        }
        if ((null == education.getStartDate() || education.getStartDate().toString().trim().length() == 0) || (null == education.getEndDate() || education.getEndDate().toString().trim().length() == 0)) {
            return new Result("504", "请填写在校时间");
        }
        if (null == education.getExperienceSchool() || education.getExperienceSchool().trim().length() == 0) {
            return new Result("505", "请填写在校经历");
        }
        if(education.getEndDate().getTime()<=education.getStartDate().getTime()){
            return new Result("506", "请填写正确的时间");
        }
        Integer count = insertService.insertEducation(education);
        if(count==0)
            return new Result("500","请重试");
        return new Result("200","添加成功");
    }
    //添加实习经历
    @PostMapping("/intern/insert")
    public Result insertIntern(Intern intern){
        System.out.println("简历id:"+intern.getResumeId());
        System.out.println(intern);
        if (null == intern.getCompanyName() || intern.getCompanyName().trim().length() == 0) {
            return new Result("501", "请填写公司名称");
        }
        if (null == intern.getCompanyIndustry() || intern.getCompanyIndustry().trim().length() == 0) {
            return new Result("502", "请填写公司行业");
        }
        if (null == intern.getTitle() || intern.getTitle().trim().length() == 0) {
            return new Result("503", "请填写职位名称");
        }
        if ((null == intern.getStartDate() || intern.getStartDate().toString().trim().length() == 0) || (null == intern.getEndDate() || intern.getEndDate().toString().trim().length() == 0)) {
            return new Result("504", "请填写在校时间");
        }
        if (null == intern.getContent() || intern.getContent().trim().length() == 0) {
            return new Result("505", "请填写实习经历");
        }
        if(intern.getEndDate().getTime()<=intern.getStartDate().getTime()){
            return new Result("506", "请填写正确的时间");
        }
        Integer count = insertService.insertIntern(intern);
        if(count==0)
            return new Result("500","请重试");
        return new Result("200","添加成功");
    }
    //添加项目经历
    @PostMapping("/project/insert")
    public Result insertProject(Project project){
        System.out.println("project="+project);
        if (null == project.getName() || project.getName().trim().length() == 0) {
            return new Result("501", "请填写项目名称");
        }
        if ((null == project.getStartTime() || project.getStartTime().toString().trim().length() == 0) || (null == project.getEndTime() || project.getEndTime().toString().trim().length() == 0)) {
            return new Result("502", "请填写项目时间");
        }
        if (null == project.getLink() || project.getLink().trim().length() == 0) {
            return new Result("503", "请填写项目链接");
        }
        if (null == project.getDescription() || project.getDescription().trim().length() == 0) {
            return new Result("504", "请填写项目描述");
        }
        if(project.getEndTime().getTime()<=project.getStartTime().getTime()){
            return new Result("506", "请填写正确的时间");
        }
        Integer count = insertService.insertProject(project);
        if(count==0)
            return new Result("500","请重试");
        return new Result("200","添加成功");
    }
    //添加获奖经历
    @PostMapping("/award/insert")
    public Result insertAward(Award award){
//        System.out.println("award:"+award);
        if (null == award.getName() || award.getName().trim().length() == 0) {
            return new Result("501", "请填写获奖名称");
        }
        if (null == award.getAwardTime() || award.getAwardTime().toString().trim().length() == 0) {
            return new Result("502", "请填写获奖时间");
        }
        Integer count = insertService.insertAward(award);
        if (count == 0){
            return new Result("500","请重试");
        }
        return new Result("200","添加成功");
    }
    //刷新简历
    @PostMapping("/refresh/update")
    public Result updateTime(Integer id){
        Integer count = resumeService.updateUpdateTimeById(id);
        if (count != 0) {
            return new Result("200", "刷新成功", id);
        }
        return new Result("400", "刷新失败");
    }
    //删除教育经历
    @PostMapping("/education/delete")
    public Result deleteEducationByIdAndResumeId(Integer educationId){
        Integer count = deleteService.deleteEducationById(educationId);
        if (count != 0) {
            return new Result("200", "删除成功");
        }
        return new Result("500", "请重试");
    }
    //删除实习经历
    @PostMapping("/intern/delete")
    public Result deleteInternByIdAndResumeId(Integer internId){
        Integer count = deleteService.deleteInternById(internId);
        if (count != 0) {
            return new Result("200", "删除成功");
        }
        return new Result("400", "删除失败");
    }
    //删除项目经历
    @PostMapping("/project/delete")
    public Result deleteProjectByIdAndResumeId(Integer projectId){
//        Integer count = resumeService.deleteProjectByIdAndResumeId(projectId,id);
        Integer count = deleteService.deleteProjectById(projectId);
        System.out.println(count);
        if (count != 0) {
            return new Result("200", "删除成功");
        }
        return new Result("400", "删除失败");
    }
    //删除获奖经历
    @PostMapping("/award/delete")
    public Result deleteAwardByIdAndResumeId(Integer awardId){
        System.out.println("awardId:"+awardId);
        Integer count = deleteService.deleteAwardIdById(awardId);
        System.out.println(count);
        if (count != 0) {
            return new Result("200", "删除成功");
        }
        return new Result("400", "删除失败");
    }
    @GetMapping("/latest")
    public Result latestResume(HttpServletRequest request){
        HttpSession session = request.getSession();
        Employee employee= (Employee)session.getAttribute("employee");
        Integer employeeId = employee.getId();
        Integer resumeId = resumeService.getLatestResumeId(employeeId);
//        System.out.println("即将转到页面：/resume/resume_make?resumeId="+resumeId);

//        return "redirect:/resume/resume_make?resumeId="+resumeId;
        //return "redirect:/redirect/index?base=r1";
        return new Result("200","成功",resumeId);
    }
    //新建简历
    @GetMapping("/create/resume")
    public Result createReaume(HttpServletRequest request,Integer id){
        HttpSession session = request.getSession();
        Employee employee = (Employee) session.getAttribute("employee");
        Integer employeeId = employee.getId();
        boolean flag = resumeService.judgeCreate(employeeId);
        if(flag)
            return new Result("501","创建失败");
        Integer resumeId = resumeService.createResume(employeeId);
        request.setAttribute("resume",resumeId);
        return new Result("200","成功",resumeId);
    }
    @PostMapping("/upload")
    @ResponseBody
    public Result upload(MultipartFile file, HttpServletRequest request){
        //获取属性
        System.out.println(123);
        String filename = file.getOriginalFilename();
        //重新生成一个新的名称
        String newFileName = UUID.randomUUID().toString()+"_"+filename;
        //获取imgs的绝对路径
        ///Users/admin/Documents/success/teaching/wksp/spring-j13/springboot-j03s/src/main/webapp/
        String path = request.getServletContext().getRealPath("/");
        //写出去
        File f = new File("D:\\Software\\nginx-1.20.1\\nginx-1.20.1\\html\\hire-front-end\\imgs\\"+newFileName);
        try {
            file.transferTo(f);
        } catch (IOException e) {
            e.printStackTrace();
            return new Result("500","文件上传失败");
        }
        avatarName = newFileName;
        return new Result("200","文件上传成功",newFileName);
    }
}
